package com.nchu.controller;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nchu.domain.dto.PageDTO;
import com.nchu.domain.dto.UserFormDTO;
import com.nchu.domain.po.Result;
import com.nchu.domain.po.User;
import com.nchu.domain.qo.UserQuery;
import com.nchu.domain.vo.UserVO;
import com.nchu.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "用户管理接口")
@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping
    @ApiOperation("新增用户")
    // RequestBody:用来接收前端传递后端的json字符串中的数据
    public Result saveUser(@RequestBody UserFormDTO userFormDTO){
        //1.转换DTO 为  PO
        User user = BeanUtil.copyProperties(userFormDTO, User.class);
        //2.新增
        userService.save(user);
        return Result.success();
    }


    @DeleteMapping("/{id}")
    @ApiOperation("删除用户")
    public Result removeById(@ApiParam("用户id") @PathVariable("id") Long userId){
        userService.removeById(userId);
        return Result.success();
    }


    @GetMapping("/{id}")
    @ApiOperation("根据id查询用户")
    public Result getById(@ApiParam("用户id") @PathVariable("id") Long userId){
        //1.查询用户
        User user = userService.getById(userId);
        //2.拷贝 把 User 复制给 UserVo
        UserVO userVO = BeanUtil.copyProperties(user, UserVO.class);
        return Result.success(userVO);
    }

    @GetMapping
    @ApiOperation("根据id查询批量用户")
    public Result getUserById(@ApiParam("用户id集合") @RequestParam("ids") List<Long> ids){
        //1.查询用户
        List<User> users = userService.listByIds(ids);

        //2.拷贝 把 users集合 复制给 UserVo
        List<UserVO> userVOList = BeanUtil.copyToList(users, UserVO.class);
        return Result.success(userVOList);
    }


    @PutMapping("/{id}/deduction/{money}")
    @ApiOperation("根据id扣减余额")
    public Result deductBalance(@PathVariable("id") Long id,@PathVariable("money") Integer money){
        userService.deductBalance(id,money);
        return Result.success();
    }


    @GetMapping("/list")
    @ApiOperation("复杂条件查询用户")
    public Result queryUser(UserQuery userQuery){
        //1.获取条件
        String username = userQuery.getName();
        Integer status = userQuery.getStatus();
        Integer minBalance = userQuery.getMinBalance();
        Integer maxBalance = userQuery.getMaxBalance();
        //MP 调用结果只需要一个list集合
        List<User> users = userService.lambdaQuery()
                .like(username != null, User::getUsername, username)
                .eq(status != null, User::getStatus, status)
                .ge(minBalance != null, User::getBalance, minBalance)
                .le(maxBalance != null, User::getBalance, maxBalance)
                .list();
        //链式编程最后一个方法来判断返回结果

        //2.拷贝 把 users集合 复制给 UserVo
        List<UserVO> userVOList = BeanUtil.copyToList(users, UserVO.class);

        return Result.success(userVOList);
    }


    @GetMapping("/by/{id}")
    @ApiOperation("根据id查询用户接口对应的地址")
    public Result queryUserAndAddressById(@ApiParam("用户id") @PathVariable("id") Long userId){
        UserVO userVO = userService.queryUserAndAddressById(userId);
        return Result.success(userVO);
    }


    @GetMapping("/by")
    @ApiOperation("根据id批量查询用户接口对应的地址")
    public Result queryUserByIdList(@ApiParam("用户id集合") @RequestParam("ids") List<Long> ids){

        return userService.queryUserByIdList(ids);
    }


    @GetMapping("/page")
    public PageDTO<UserVO> queryUserPage(UserQuery query){
        return userService.queryUserPage(query);
    }


}
